package app_dao

import (
	"fmt"
	"hft/Database/admin"
	"hft/Database/admin/country_setting"
	"hft/Http/Services/front/app/app_model"
	"hft/global"
	"strings"

	"gorm.io/gorm"
)

type AppIndex struct{}

// 首页今日资讯
func (*AppIndex) GetArticle(db *gorm.DB, regionLink string) (list []*app_model.TodayNews) {
	var news []*admin.ArticleBase
	global.OC_DB.Select("name,cover,author,create_time,id").Where("region_link = ?", regionLink).Order("create_time desc").Limit(10).Find(&news)
	var newsList = make([]*app_model.TodayNews, 0)
	for _, v := range news {
		newsList = append(newsList, &app_model.TodayNews{ID: v.ID, Title: v.Name, Cover: global.OC_CONFIG.AliyunOSS.BucketUrl + v.Cover, Author: v.Author, CreateTime: v.CreateTime.Format("2006-01-02")})
	}
	return newsList
}

// 推荐楼盘
func (*AppIndex) GetLouPanList(db *gorm.DB, regionLink string) (list []*app_model.LouPan) {
	var louList []*admin.LouPan
	db.Preload("Tags").Preload("Disadvantage", func(db *gorm.DB) *gorm.DB {
		return db.Order("sort desc")
	}).Where("region_link LIKE ?", fmt.Sprintf("%v%%", regionLink)).Order("sort desc").Limit(10).Find(&louList)
	var newLouList = make([]*app_model.LouPan, 0)
	for _, v := range louList {
		var belongStr = strings.Split(v.BelongName, "-")
		var BelongName = belongStr[2]
		var tags []string
		var rank = make([]*app_model.Disadvantage, 0)
		for _, j := range v.Tags {
			tags = append(tags, j.Name)
		}
		for _, j := range v.Disadvantage {
			rank = append(rank, &app_model.Disadvantage{Title: j.Name, Cover: global.OC_CONFIG.AliyunOSS.BucketUrl + j.Cover, Color: j.TextColor})
		}
		newLouList = append(newLouList, &app_model.LouPan{Name: app_model.Name{ID: v.ID, Name: v.Name}, Cover: global.OC_CONFIG.AliyunOSS.BucketUrl + v.Cover, BelongName: BelongName, Tag: tags, Status: v.Status, ReferencePrice: v.ReferencePrice, Rank: rank})
	}
	return newLouList
}

// 新房
func (*AppIndex) GetLouPanNewList(db *gorm.DB, regionLink string) (list []*app_model.LouPan) {
	var louList []*admin.LouPan
	db.Joins("LEFT JOIN hft_loupan_tese ON `hft_loupans`.`id` = `hft_loupan_tese`.`lou_pan_id`").Preload("Tags").Preload("Disadvantage", func(db *gorm.DB) *gorm.DB {
		return db.Order("sort desc")
	}).Where("region_link LIKE ?", fmt.Sprintf("%v%%", regionLink)).Where("`hft_loupan_tese`.`lou_pan_te_se_name` = \"力荐新房\"").Order("sort desc").Limit(10).Find(&louList)
	var newLouList = make([]*app_model.LouPan, 0)
	for _, v := range louList {
		var belongStr = strings.Split(v.BelongName, "-")
		var BelongName = belongStr[2]
		var tags []string
		var rank = make([]*app_model.Disadvantage, 0)
		for _, j := range v.Tags {
			tags = append(tags, j.Name)
		}
		for _, j := range v.Disadvantage {
			rank = append(rank, &app_model.Disadvantage{Title: j.Name, Cover: global.OC_CONFIG.AliyunOSS.BucketUrl + j.Cover, Color: j.TextColor})
		}
		newLouList = append(newLouList, &app_model.LouPan{Name: app_model.Name{ID: v.ID, Name: v.Name}, Cover: global.OC_CONFIG.AliyunOSS.BucketUrl + v.Cover, BelongName: BelongName, Tag: tags, Status: v.Status, ReferencePrice: v.ReferencePrice, Rank: rank})
	}
	return newLouList
}

// 表情
func (*AppIndex) GetEmojiList(db *gorm.DB) (list []*app_model.Emoji) {
	var emojiList []*country_setting.Emoji
	db.Order("sort desc").Find(&emojiList)
	var newEmojiList = make([]*app_model.Emoji, 0)
	for _, v := range emojiList {
		newEmojiList = append(newEmojiList, &app_model.Emoji{
			Emoji:    v.Emoji,
			TextShow: v.TextShow,
		})
	}
	return newEmojiList
}

var AppIndexDao = new(AppIndex)
